import { LOGIN_TOKEN } from "@/global/constants"
import { localCache } from "@/utils/cacheData"
import { createRouter, createWebHashHistory } from "vue-router"
import localRoutes from "@/mock/localRoutes"

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "/main"
    },
    {
      path: "/login",
      component: () => import("../views/login/login.vue")
    },
    {
      path: "/main",
      component: () => import("../views/main/main.vue"),
      children: [
        // 1 项目概览 project-overview
        {
          path: "projectOverview/productOverview",
          name: "productOverview",
          component: () => import("@/views/main/project-overview/product-overview.vue"),
          children: []
        },
        {
          path: "projectOverview/visualOverview",
          name: "visualOverview",
          component: () => import("@/views/main/project-overview/visual-overview.vue"),
          children: []
        },
        // 2 产品管理 product-management
        // product-information 产品信息
        {
          path: "productManagement/productInformation/equipmentControl",
          name: "equipmentControl",
          component: () =>
            import("@/views/main/product-management/product-information/equipment-control.vue")
        },
        {
          path: "productManagement/productInformation/robotPermission",
          name: "robotPermission",
          component: () =>
            import("@/views/main/product-management/product-information/robot-permission.vue")
        },
        //  data-overview 数据监控
        {
          path: "productManagement/dataOverview/robotMalfunction",
          name: "robotMalfunction",
          component: () =>
            import("@/views/main/product-management/data-overview/robot-malfunction.vue")
        },
        {
          path: "productManagement/dataOverview/robotDataAnalyse",
          name: "robotDataAnalyse",
          component: () =>
            import("@/views/main/product-management/data-overview/robot-dataAnalyse.vue")
        },
        // 3 地图信息 map-management
        {
          path: "mapManagement/mapDrive",
          name: "mapDrive",
          component: () => import("@/views/main/map-management/map-drive.vue")
        }
      ]
    },

    {
      path: "/:pathMatch(.*)",
      component: () => import("../views/404-NotFound/404-NotFound.vue")
    }
  ]
})

router.beforeEach((to, form) => {
  // 只有登录成功（有token），才会真正进入到main页面
  const token = localCache.getCache(LOGIN_TOKEN)
  // startsWith以什么字符开头
  if (to.path.startsWith("/main") && !token) {
    return "/login"
  }

  // 如果进入到main，默认进入第一个menu
  if (to.path == "/main") {
    return "/main/projectOverview/productOverview"
  }
})

export default router
